<?php namespace Admin\Controller; 

class GoodsListController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\GoodsList;
	}
	//货品列表
	public function index(){
		$gid=Q('get.gid',0,'intval');
		$goodsAttrModel=new \Common\Model\GoodsAttr;
		$data=$goodsAttrModel
			 ->join('type_attr','type_attr_taid','=','taid')
			 ->where("goods_gid={$gid} AND class=1")
			 ->groupBy('taid')
			 ->get();
		//添加货品的时候下拉菜单所选择的内容(一定是从商品属性表获得)
		foreach($data as $k=>$v){
//			$data[$k]['combine']=explode(',', $vv=rtrim($v['combine'],","));
			$data[$k]['value']=$goodsAttrModel
							  ->where("type_attr_taid={$v['taid']} AND goods_gid={$gid}")
							  ->get();
							
		}
		//p($data);
		View::with('data',$data);
		//显示货品
        $listData = $this->model->where("goods_gid={$gid}")->get();
		
//		p($listData);
//		$comData=[];
        foreach ($listData as $k => $v) {
        	$vv = explode(',', $v['combine']);
			foreach ($vv as $value) {
				//p($value);exit;
				$listData[$k]['value'][] = Db::table("goods_attr")->where("gaid={$value}")->first();
			}
            
        }
//		p($comData);
//		p($listData);
//      View::with('comData',$comData);

		
        View::with('listData',$listData);
        
		View::make();
	}
	public function add(){
//		p($_POST);
		
		//$glid=Q('get.gid',0,'intval');
//		$type_attr_taid=Q('get.type_attr_taid',0,'intval');
		exit($this->model->stroe());
		
	}
	
	public function del(){
		$glid=Q('get.glid',0,'intval');
		$this->model->where("glid={$glid}")->delete();
		View::success('删除成功');
	}
	
	public function edit(){
		//处理旧数据

		$glid=Q('get.glid',0,'intval');
		$oldData=$this->model
				->where("glid={$glid}")
				->find();
		p($oldData);
		$goodsAttrModel=new \Common\Model\GoodsAttr;
		$gid=Q('get.gid',0,'intval');
//		P($gid);EXIT;
		$data=$goodsAttrModel
			 ->join('type_attr','type_attr_taid','=','taid')
			 ->where("goods_gid={$gid} AND class=1")
			 ->groupBy('taid')
			 ->get();
		 foreach($data as $k=>$v){
//			$data[$k]['combine']=explode(',', $vv=rtrim($v['combine'],","));
		$data[$k]['value']=$goodsAttrModel
						  ->where("type_attr_taid={$v['taid']} AND goods_gid={$gid}")
						  ->get();
							
		}
//		p($data);
		View::with('data',$data);
		//将组合ID变成数组
		$combine=explode(',', $oldData['combine']);
		p($combine);
		View::with('combine',$combine);
		View::with('oldData',$oldData);
		View::make();
	}
	
}